<?php
   defined('_JEXEC') or die( 'Restricted access' );

   class modRWCardsHelper{
      function getCards(&$params){

         $quant = $params->get('quantity', '1');
		 //thumb_suffix Hack
		 $suffix = '@' . $params->get("thumbnail_suffix", 'rwcards' );

         if($params->get('orderby') != "newest") {
            $orderby = "RAND()";
         } else {
            $orderby = "id desc";
         }

         if($params->get('category') == "") {
            $category = "";
         } else {
            $cat_id_new = str_replace(",", " OR category_id = ", $params->get('category'));
            $category = "AND category_id = ".$cat_id_new."";
         }		 
	

         function cut($descr,$count,$cardurl) {
            if(strlen($descr) > $count) {
               $re = preg_replace("/[^ ]*$/", '<a href="'.$cardurl.'"><b>[...]</b></a>', substr($descr, 0, $count), 1);
            } else {
               $re = $descr;
            }
            return $re;
         }

         global $mainframe;
         $db  = &JFactory::getDBO();
         $query = "SELECT
                      id,
                      picture,
                      description,
                      published,
                      category_id
                   FROM
                      #__rwcards
                   WHERE
                      published = 1
                      ".$category."
                   ORDER BY
                      ".$orderby."
                   LIMIT
                      ".$quant."";
         $db->setQuery( $query );
         $rows = $db->loadObjectList();;
         $cards = $params->get('pre_text');
         $cards .= "<table>";
         if ($rows) {
            foreach ($rows as $row){
               $cardurl = 'index.php?option=com_rwcards&amp;controller=rwcardsfilloutcard&amp;id='.$row->id.'&amp;category_id='.$row->category_id;
               $short_descr = cut($row->description,$params->get('strlen'),$cardurl);
               $new_picture = strtolower(substr($row->picture, 0, -4) ). "". $suffix. strtolower(substr($row->picture, strrpos($row->picture, ".")) );
               //$new_picture = strtolower(pathinfo($row->picture, PATHINFO_FILENAME));
               $cards .= "<tr><td><a href=\"".$cardurl."\">";
               $cards .= "<p style=\"text-align:center; margin:0px; padding:0px;\"><img style=\"border:1px black solid;\" src=\"".JURI::base()."images/stories/cards/".$new_picture."\"></p>";
               $cards .= "</a></td></tr>";
               if($params->get('descr') == 1) {
                  $cards .= "<tr><td><b>".JText::_('description').":</b></td></tr>";
                  if($row->description != "") {
                     $cards .= "<tr><td>".$short_descr."</td></tr>";
                  } else {
                     $cards .= "<tr><td><i>".JText::_('not found')."</i></td></tr>";
                  }
               }
            }
         }
         $cards .= "</table>\n";
         $cards .= $params->get('post_text');
         return $cards;
      }
   }
?>